//- Copyright (C) 2018-2022 Chatopera Inc, <https://www.chatopera.com>
//- 
//- Licensed under the Apache License, Version 2.0 (the "License");
//- you may not use this file except in compliance with the License.
//- You may obtain a copy of the License at
//- 
//-  http://www.apache.org/licenses/LICENSE-2.0
//- 
//-  Unless required by applicable law or agreed to in writing, software
//- distributed under the License is distributed on an "AS IS" BASIS,
//-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//- See the License for the specific language governing permissions and
//- limitations under the License.
extends /apps/include/layout.pug
include /mixins/client.mixin.pug

block append head
    style.
        .ukefu-report{
                position: absolute;
                margin-top: 10px;
                width: auto;
            }
            .ukefu-report td{
                max-width:150px;
                min-width:50px;
            }
            .layui-form-label{
                padding-left: 0px;
            }

block content
    .layui-side.layui-bg-black
        .layui-side-scroll
            include /apps/service/include/left.pug
    .layui-body
        .layui-side-scroll
            .row
                .col-lg-12
                    h1.site-h1(style='border-top:1px solid #e6e6e6;')
                        | 客服坐席
                        span
                            .ukefu-bt-text-content
                                form.layui-form(name='search', action='/service/stats/agent.html', method='get', style='float: left;margin-right:5px;')
                                    .layui-form-item
                                        .layui-inline(style='margin-right:0px;')
                                            .layui-input-inline(style='width: auto')
                                                label.layui-form-label(style='width: auto;') 技能组：
                                                .layui-input-inline(style='width: auto;margin-right:0px;')
                                                    select.ukefu-input#skill(name='skill', lay-ignore, onchange='renderAgentsByOrgan(this.value)')
                                                        option(value) 请选择技能组
                                                            if skillGroups
                                                                for tpskill in skillGroups
                                                                    option(value= tpskill.id, selected =(skill == tpskill.id ? "selected" : false)) #{tpskill.name}
                                                        //<#if skillGroups??>
                                                        //<#list skillGroups as tpskill>
                                                        //    <option value="${tpskill.id}" <#if skill?? && skill == tpskill.id>selected="selected"</#if>>${tpskill.name!''}</option>
                                                        //</#list>
                                                        //</#if>
                                        .layui-inline(style='margin-right:0px;')
                                            .layui-input-inline(style='width: auto;margin-right:0px;')
                                                label.layui-form-label(style='width: auto;') 坐席：
                                                .layui-input-inline(style='width: auto;margin-right:0px;')
                                                    select.ukefu-input#agent(name='agent', lay-ignore)
                                                        option(value) 请选择坐席
                                                            if agentList
                                                                for useragent in agentList
                                                                    option(value= useragent.id, selected= (agent == useragent.id ? "selected" : false)) #{useragent.uname}
                                                            //<#if agentList??>
                                                            //<#list agentList as useragent>
                                                            //    <option value="${useragent.id}" <#if agent?? && agent == useragent.id>selected="selected"</#if>>${useragent.uname!''}</option>
                                                            //</#list>
                                                            //</#if>
                                        .layui-inline(style='margin-left:10px;')
                                            .layui-input-inline(style='width: auto;margin-right:0px;')
                                                label.layui-form-label(style='width: auto;') 日期范围：
                                                .layui-input-inline(style='width: auto;margin-right:0px;')
                                                    input#start_time.layui-input.ukefu-input(name='begin', placeholder='开始日期', value= begin)
                                                label.layui-form-label(style='width: auto;') ~
                                                .layui-input-inline(style='width: auto;margin-right:0px;')
                                                    input#end_time.layui-input.ukefu-input(name='end', placeholder='结束日期', value= end)
                                        .layui-inline(style='margin-right:0px;')
                                            .layui-input-inline(style='width: auto;margin-right:0px;')
                                                .layui-input-inline(style='width: auto;margin-right:0px;')
                                                    button.layui-btn.layui-btn-small.layui-btn-original(type='submit', style='color:#ffffff;')
                                                        i.layui-icon 
                                .layui-btn-group.ukefu-btn-group(style='margin-top:2px;')
                                    a(href='/service/stats/agent/exp.html?t=' + pugHelper.currentTimeMillis() + '&skill=' + skill + '&agent=' + agent + '&begin=' + begin + '&end=' + end)
                                        button.layui-btn.layui-btn-small.dropdown-menu
                                            i.kfont 
                                            |  导出
                    include /apps/service/stats/report.pug

    script(src='/js/CSKeFu_Rest_Request.v1.js')
    script.
        //Demo
        layui.use('form', function(){
          var form = layui.form();
          form.render(); //更新全部
        });
        layui.use('laydate', function() {
            var laydate = layui.laydate;
        });

        function getNowFormatDate() {
            var date = new Date();
            var seperator = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + seperator + month + seperator + strDate;
            return currentdate;
        }

        var start = {
            istime: true,
            format: 'YYYY-MM-DD',
            max:getNowFormatDate(),//默认最大值为当前日期
            istoday: true,
            choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
            }
        };

        var end = {
            istime: true,
            format: 'YYYY-MM-DD',
            max:getNowFormatDate(),//默认最大值为当前日期
            istoday: true,
            choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };

        document.getElementById('start_time').onclick = function () {
            start.elem = this;
            laydate(start);
        };
        document.getElementById('end_time').onclick = function () {
            end.elem = this;
            laydate(end);
        };

        function renderAgentsByOrgan(organid){
                restApiRequest({
                    silent: true,
                    path: 'user',
                    data: {
                        ops: 'findByOrgan',
                        organ: organid
                    }
                }).then(function(result){
                    if(result.rc == 0){
                        $("#agent").html('<option value="">所有坐席</option>');
                        for (var i=0 ; i<result.data.length ; i++) {
                            $("#agent").prepend('<option value="' + result.data[i].id + '">' + result.data[i].uname + '</option>')
                        };
                    }
                }, function(error){
                    // 服务器异常
                    top.layer.msg('服务器抽风，请稍后再试！', {icon: 2, offset: 'b', time: 3000})
                })
        }